$import("com.joypard.platform.security.resource.ResourceDI");
$import("com.joypard.platform.security.resource.ResourceChildView");
$import("com.joypard.platform.base.appsystem.AppSystemCompoent");

/**
 * 资源资源
 */
K.defView("com.joypard.platform.security.resource.ResourceView", {
	resourceCtrl : null,
	resourceChildView : null,

	/**
	 * 构造函数
	 */
	ResourceView : function() {
		// 子资源视图
		this.resourceChildView = ResourceChildView.instance(this);

		E.addListener("updateResSuccessEvent", this.onUpdateResourceSuccess, this);
		E.addListener("delResSuccessEvent", this.onDelResourceSuccess, this);
	},

	render : function() {
		// 左侧两颗小树管理面板
		this.westPanel = this.createWestPanel();
		this.resourceTreePanel = this.createResourceTreePanel();// 资源树
		this.appSystemTreePanel = this.createAppSystemTreePanel();// 系统树

		this.westPanel.add(this.appSystemTreePanel);
		this.westPanel.add(this.resourceTreePanel);

		// 中央管理面板
		this.centerPanel = this.createCenterPanel(); // 标签页
		this.resourceEditTabPanel = this.createResourceEditTabPanel();// 资源编辑面板
		this.resourceChildGridPanel = this.resourceChildView.getResourceChildGridPanel(); // 子资源列表面板
		this.resourceChildEditPanel = this.resourceChildView.getResourceChildEditPanel(); // 子资源编辑面板

		this.centerPanel.add(this.resourceEditTabPanel);
		this.centerPanel.add(this.resourceChildGridPanel);

		// 主面板
		new K.Ext.MainPanel({
			items : [ this.westPanel, this.centerPanel ]
		});
	},
	/**
	 * 两颗小树管理面板
	 */
	createWestPanel : function() {
		var options = {
			width : "40%",
			region : "west",
			bodyCssClass : "def-bg", // 背景色
			border : false,
			layout : "hbox",
			layoutConfig : {
				align : "stretch"
			}
		};
		return new K.Ext.Panel(options);
	},
	/**
	 * 资源树
	 */
	createResourceTreePanel : function() {
		var options = {
			title : "系统资源",
			iconCls : "p-panel",
			autoScroll : true,
			margins : "2 2 3 0",
			frame : true,
			flex : 1,
			region : "center",
			loader : ResourceDI.createResTreeLoader()
		};
		var resourceTreePanel = new K.Ext.tree.TreePanel(options);
		resourceTreePanel.getSelectionModel().on("selectionchange", function(selModel, node) {
			if (node) {
				E.fireEvent("resNodeClick", node);
				this.onResNodeClick(node);
			}
		}, this);
		return resourceTreePanel;
	},

	/**
	 * 创建应用系统树
	 * 
	 * @returns
	 */
	createAppSystemTreePanel : function() {
		var appSystemTreePanel = AppSystemCompoent.createSelectAppSystemPanel({ // 应用系统
			margins : "2 2 3 2",
			width : "50%",
			rootVisible : false
		});

		appSystemTreePanel.getSelectionModel().on("selectionchange", function(selModel, node) {
			var root = ResourceDI.createAsyncAppRootNode(node.id);
			if (root) {
				this.resourceTreePanel.setRootNode(root);
				root.expand(false, true, function() {
					root.fireEvent("click", root);
				});
			} else {
				this.resourceTreePanel.getRootNode().getUI().hide();
				this.curRes = this.resourceChildView.curRes = ResourceDI.createAsyncRootNode();
				this.onAddChildResource();
				this.resourceChildGridPanel.getStore().removeAll();
				K.err("该系统无任何资源", this.resourceTreePanel.body);
			}
		}, this);

		return appSystemTreePanel;
	},
	/**
	 * 创建中间标签页面板
	 */
	createCenterPanel : function() {
		var options = {
			activeTab : 0,
			margins : "2 2 3 0",
			region : "center"
		};
		return new K.Ext.TabPanel(options);
	},

	/**
	 * 创建资源编辑面板
	 */
	createResourceEditTabPanel : function() {
		var options = {
			title : "资源信息",
			iconCls : "p-tab",
			autoScroll : true,
			tbar : [ {
				text : "删除",
				iconCls : "delete",
				handler : K.createDelegate(this.onDelResource, this)
			}, {
				text : "保存",
				iconCls : "save",
				handler : K.createDelegate(this.onSaveResource, this)
			}, {
				id : "addResChildBtn",
				text : "新增子资源",
				iconCls : "add",
				handler : K.createDelegate(this.onAddChildResource, this)
			} ]
		};
		return new K.Ext.Panel(options);
	},

	// 事件定义
	/**
	 * 加载资源信息,判断当前节点类型并设置相关按钮、标签项的状态
	 */
	onResNodeClick : function(resNode) {
		this.curRes = resNode;
		this.resourceEditTabPanel.load({
			method : "GET",
			scripts : true,
			url : ResourceDI.urls.updateRes,
			params : {
				resId : this.curRes.id
			}
		});

		var addResChildBtn = this.resourceEditTabPanel.getTopToolbar().get("addResChildBtn");
		if (this.curRes.attributes.type == "C" || this.curRes.attributes.type == "E") {
			addResChildBtn.disable();
			this.centerPanel.activate(this.resourceEditTabPanel);
			this.resourceChildGridPanel.disable();
			this.resourceChildEditPanel.disable();
		} else {
			addResChildBtn.enable();
			this.resourceChildGridPanel.enable();
			this.resourceChildEditPanel.enable();
			this.resourceChildGridPanel.getStore().load({
				url : ResourceDI.urls.getChildrens,
				params : {
					parentResId : this.curRes.id
				}
			});
		}
	},
	/**
	 * 点击保存按钮
	 */
	onSaveResource : function() {
		var form = $("#resForm", this.resourceEditTabPanel.getEl().dom), state = form.attr("state"), eventName = null;
		if ("update" == state) {
			eventName = "updateResEvent";
		} else if ("add" == state) {
			eventName = "addResEvent";
		}

		if (eventName) {
			E.fireEvent(eventName, form);
		}
	},
	/**
	 * 删除资源
	 */
	onDelResource : function() {
		if (this.curRes) {
			if (!confirm("删除资源同时会删除该资源的子资源,您确定要删除吗?")) {
				return;
			}
			E.fireEvent("delResEvent", this.curRes);
		} else {
			K.err("请选择要删除的资源", this.resourceEditTabPanel);
		}
	},
	/**
	 * 新增子资源
	 */
	onAddChildResource : function() {
		this.showResourceChildEditPanel(this.curRes ? this.curRes.id : "0");
	},

	onUpdateResourceSuccess : function(output) {
		var resource = output.getParameter("resource");
		K.Ext.flyTree(this.resourceTreePanel).setNodeText(resource.id, resource.name).updateIcon(resource.id, resource.iconCls);
		K.m("修改成功").show(this.resourceEditTabPanel.tbar);
	},
	onDelResourceSuccess : function(output) {
		var resIds = output.getParameter("resIds");
		K.m("删除成功").show(this.resourceEditTabPanel.tbar);
		K.Ext.flyTree(this.resourceTreePanel).autoSelectByDelete(resIds);
	},

	// 函数定义
	/**
	 * 激活子资源编辑标签
	 */
	activeResourceChildEditPanel : function() {
		if (!this.resourceChildEditPanel.rendered) {
			this.centerPanel.add(this.resourceChildEditPanel);
		}
		this.centerPanel.activate(this.resourceChildEditPanel);
	},
	loadResourceChildEditPanel : function(id) {
		this.resourceChildEditPanel.load({
			method : "GET",
			scripts : true,
			url : ResourceDI.urls.addRes,
			params : {
				parentResId : id,
				appId : this.appSystemTreePanel.getSelectionModel().getSelectedNode().id
			}
		});
	},
	showResourceChildEditPanel : function(id) {
		this.activeResourceChildEditPanel();
		this.loadResourceChildEditPanel(id);
	}
});
